package com.sc.auction.auction.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sc.auction.auction.bean.AuctionVo;
import com.sc.auction.auction.bean.PageQuery;
import com.sc.auction.auction.bean.SuperAuctionVo;
import com.sc.auction.auction.feign.OrderFeignService;
import com.sc.auction.auction.feign.UserFeignService;
import com.sc.auction.auction.pojo.Atuctionbargain;
import com.sc.auction.auction.pojo.Auction;
import com.sc.auction.auction.pojo.Toppricee;
import com.sc.auction.auction.service.AtuctionbargainService;
import com.sc.auction.auction.service.AuctionService;
import com.sc.auction.auction.service.SmsService;
import com.sc.auction.auction.Lock.ToppriceeService;
import com.sc.auction.common.exception.AuctionException;
import com.sc.auction.common.pojo.OrderVo;
import com.sc.auction.common.pojo.User;
import com.sc.auction.common.util.R;
import com.sc.auction.common.util.Time;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author pgc
 * @since 2020-09-01
 */
@CrossOrigin
@RestController
@RequestMapping("/auction/auction")
public class AuctionController extends BaseController{
    @Autowired
    AuctionService auctionService;
    @Autowired
    ToppriceeService toppriceeService;
    @Autowired
    OrderFeignService orderFeignService;
    @Autowired
    UserFeignService userFeignService;
    @Autowired
    SmsService smsService;
    @Autowired
    AtuctionbargainService atuctionbargainService;


    @ApiOperation("修改状态")
    @GetMapping(value = "/updateAuction")
    public R updateAuction(@ApiParam(value = "拍品id") @RequestParam("auctionid") Integer auctionid){
        System.out.println("进入修改状态2的方法");
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",auctionid);
        Auction auction=auctionService.getOne(queryWrapper);
        auction.setStatusId(2);
        auctionService.update(auction,queryWrapper);

        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("auctionid",auctionid);
        queryWrapper1.orderByDesc("auctiontime");
        queryWrapper1.last("limit 0,1");
        Toppricee toppricee=toppriceeService.getOne(queryWrapper1);
        if (toppricee!=null){
            return R.ok().data("topprice",toppricee);
        }else {
            return R.error();
        }


    }

    @ApiOperation("结束拍卖修改状态")
    @GetMapping(value = "/updateAuction1")
    public R updateAuction1(@ApiParam(value = "拍品id") @RequestParam("auctionid") Integer auctionid){
        System.out.println("进入修改状态3的方法");
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",auctionid);
        Auction auction=auctionService.getOne(queryWrapper);
        QueryWrapper queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("auctionid",auctionid);
        queryWrapper1.orderByDesc("auctiontime");
        queryWrapper1.last("limit 0,1");
        Toppricee toppricee=toppriceeService.getOne(queryWrapper1);
        if (toppricee!=null){
            //修改状态
            auction.setStatusId(3);
            auctionService.update(auction,queryWrapper);


            //订单
            Object g =orderFeignService.getOrderByAuctionId(toppricee.getAuctionid()).getData().get("order");
            OrderVo orders= JSON.toJavaObject((JSONObject)g,OrderVo.class);
            if(orders==null||orders.equals("")){

                //记录
                Atuctionbargain atuctionbargain=new Atuctionbargain();
                atuctionbargain.setAuctionId(auctionid);
                atuctionbargain.setConfirmTime(toppricee.getAuctiontime());
                atuctionbargain.setTurnoverTime(toppricee.getAuctiontime());
                atuctionbargain.setTradingVolume(toppricee.getMoney());
                atuctionbargain.setUserId(toppricee.getUser());
                atuctionbargain.setCreatedBy("lsc");
                Date date=new Date();
                atuctionbargain.setCreationDate(date);
                atuctionbargainService.save(atuctionbargain);
            //增加订单
            orderFeignService.insertOrder(toppricee.getUser(),toppricee.getMoney(),toppricee.getAuctionid());
            }
            R r=userFeignService.getUserById(toppricee.getUser());
            User user=JSON.toJavaObject((JSONObject)r.getData().get("user"),User.class);
            try {
                //发送短信
                smsService.send(user.getTelephone(),"success_auction");
            } catch (ClientException e) {
                e.printStackTrace();
            }
            return  R.ok().data("topprice",toppricee).data("user",user.getUsername());
        }else{
            //如果没有那么就流派
            auction.setStatusId(4);
            auctionService.update(auction,queryWrapper);
            return  R.ok().data("user","流拍");
        }
    }


    @ApiOperation("查询所有拍品")
    @GetMapping(value = "/findAll")
    public R getAll(){
       List list=auctionService.list();
        return R.ok().data("allAuction",list);
    }

    @ApiOperation("查询拍卖品")
    @GetMapping(value = "/getAuction")
    public R getAuction(@ApiParam(value = "拍品id") @RequestParam("auctionid") Integer auctionid){
        return  auctionService.findAuctionInfoById(auctionid);
    }

    @ApiOperation("增加拍品")
    @PostMapping(value = "/postAuction")
    public R postAuction(@ApiParam(value = "拍品")@RequestBody Auction auction){
//        System.out.println(auction);
        auction.setCreationDate(Time.getDate());
        boolean a=auctionService.save(auction);
        if(a){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("删除拍品")
    @DeleteMapping(value = "/deleteAuction")
    public R deleteAuction(@ApiParam(value = "拍品")Auction auction ){
        QueryWrapper queryWrapper=new QueryWrapper();
        //逻辑删除
        auction.setStatusId(0);
        queryWrapper.eq("id",auction.getId());
        boolean a=auctionService.update(auction,queryWrapper);
        if(a){
            return R.ok();
        }else {
            return R.error();
        }
    }

    @ApiOperation("修改拍品")
    @PutMapping(value = "/putAuction")
    public R putAuction(@ApiParam(value = "拍品")Auction auction ){
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("id",auction.getId());
        auction.setModifyDate(new Date());
        boolean a=auctionService.update(auction,queryWrapper);
        if(a){
            return R.ok();
        }else {
            return R.error();
        }
    }
    @ApiOperation("得到待拍六个")
    @GetMapping("/getSixAuctions")
    public R getSixAuctions(){
       return  R.ok().data("six",this.auctionService.findAuctionSixByTime());
    }


    @ApiOperation("得到已开拍六个")
    @GetMapping("/getSixAlreadyAuctions")
    public R getSixAlreadyAuctions(){
        return  R.ok().data("six",this.auctionService.findAuctionSixAlreadyByTime());
    }

    @ApiOperation("得到已成交六个")
    @GetMapping("/getSixBargainAuctions")
    public R getSixBargainAuctions() throws AuctionException {
        List<AuctionVo> bargainSixAuction = auctionService.findBargainSixAuction();
        if (bargainSixAuction!=null&&bargainSixAuction.size()>0){
            return R.ok().data("six",bargainSixAuction);
        }else {
            throw  new AuctionException(201,"未查询到相关信息");
        }
    }


    @ApiOperation("未开拍列表")
    @GetMapping("/getWaitList")
    public R getWaitList(@ApiParam(name = "page",defaultValue = "1",required = true)@RequestParam("page") long page,
                         @ApiParam(name="limit",defaultValue = "12",required = true)@RequestParam("limit") long limit,PageQuery pageQuery) throws AuctionException {
        SuperAuctionVo superAuctionVo = auctionService.getWaitList(page,limit,pageQuery);
//        System.out.println(superAuctionVo);
        if (superAuctionVo!=null){
            return R.ok().data("superAuctionVo",superAuctionVo);
        }else {
            throw new AuctionException(201,"资源获取失败，重新加载中");
        }
    }


}

